// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false


//axios的使用
import axios from "axios"
Vue.prototype.axios=axios  //全局axios对象设置
axios.defaults.headers.post['Content-Type'] = 'application/json';
// axios.defaults.headers.common['token'] = token;
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';//配置请求头



// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
  const token=localStorage.getItem("token");
  if (token==null||token == 0  ) {
    console.log(config.url);
    //需要拦截的请求集合
    let urls = {
      '/api/userHomePageController/getSelfInfo':'',
      '/api/userHomePageController/getUserInvestVo':'',
      '/api/userHomePageController/getuserZong':'',
      '/api/details/addInvest':''
      }
    if (config.url in urls){
      config.url = 'asdasdasdasd';
    }
  }
  if(token){
    config.headers.common['token'] = token;
  }
  return config
},
error => {
  return Promise.reject(error);
});



// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
  if (response.data && response.data.code) {
    if (parseInt(response.data.code) === 108 || parseInt(response.data.code) === 109 || response.data.msg === 'TOKEN失效，请重新登录' || response.data.msg === 'TOKEN不存在') {
      //未登录
      response.data.msg = "登录信息已失效，请重新登录";
    }
    if (parseInt(response.data.code) === -1) {
      console.log("请求失败");
    }
  }
  return response;
}, function (error) {
  // Do something with response error
  console.log("服务器连接失败");
})




//时间的配件
import moment from 'moment'
Vue.prototype.$moment = moment

import VueRouter from 'vue-router'
Vue.use(VueRouter)

import route from './route'

//配置路由
const router = new VueRouter({
  routes :route,
})

// router.beforeEach(function(to, from, next) {
//   alert(localStorage.getItem("token"))
//   if (localStorage.getItem("token")) {
//     alert("未登录")
//     if (to.path === '/api/details/addComment'||to.path === '/api/details/addInvest' ) {
//       alert("请先登录")
//       return next('../')
//     }
//   }
//   next()
// })

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);



/* eslint-disable no-new */
new Vue({
  router,
  el: '#app',
  components: { App },
  template: '<App/>',
  render: h => h(App)
})
